apply(), lapply(), sapply(), and tapply()

R
Author

Tony Duan

Published

January 2, 2024

Code
library(tidyverse)

1 apply()

Code
inputdata=(iris[1:4, 1:3])
inputdata
  Sepal.Length Sepal.Width Petal.Length
1          5.1         3.5          1.4
2          4.9         3.0          1.4
3          4.7         3.2          1.3
4          4.6         3.1          1.5

rows = 1, columns = 2, both = c(1, 2)

calculate columns mean

Code
column_means <- apply(inputdata, 2, mean)
print(column_means)
Sepal.Length  Sepal.Width Petal.Length 
       4.825        3.200        1.400 

calculate rows mean

Code
column_means <- apply(inputdata, 1, mean)
print(column_means)
       1        2        3        4 
3.333333 3.100000 3.066667 3.066667 

2 lapply()

lapply() is the speed demon, applying a function to each element of a list or vector and returning a list of results.

Calculate the median of each petal length in a list of lists:

Code
petal_lengths <- list(c(1.4, 1.5, 1.6), c(4.4, 4.5, 4.6))
petal_lengths
[[1]]
[1] 1.4 1.5 1.6

[[2]]
[1] 4.4 4.5 4.6
Code
petal_medians <- lapply(petal_lengths, median)
petal_medians
[[1]]
[1] 1.5

[[2]]
[1] 4.5

3 sapply()

sapply() is like lapply(), but it tries to simplify the output. If all results are of the same type (e.g., numeric), it returns a vector instead of a list.

Code
petal_medians <- sapply(petal_lengths, median)
petal_medians
[1] 1.5 4.5

4 tapply()

group the Sepal.Length column by the Species column (using iris$Species) and calculate the mean (mean) for each group. The results are stored in sepal_length_by_species

Code
sepal_length_by_species <- tapply(iris$Sepal.Length, iris$Species, mean)
print(sepal_length_by_species)
    setosa versicolor  virginica 
     5.006      5.936      6.588 

5 Reference

https://www.r-bloggers.com/2024/02/conquering-rs-apply-family-your-guide-to-apply-lapply-sapply-and-tapply/

https://www.youtube.com/watch?v=96-eG79fnms